// Decompiled by DJ v3.5.5.77 Copyright 2003 Atanas Neshkov  Date: 2009-3-27 22:23:17
// Home Page : http://members.fortunecity.com/neshkov/dj.html  - Check often for new version!
// Decompiler options: packimports(3) 
// Source File Name:   MessageDaoImpl.java

package dao.impl;

import dao.MessageDao;
import domain.Message;
import java.util.List;
import org.hibernate.*;

// Referenced classes of package dao.impl:
//            HibernateSessionFactory

public class MessageDaoImpl
    implements MessageDao
{

    public MessageDaoImpl()
    {
    }

    public Message findById(int id)
    {
        Session session = null;
        try {
			Message message1;
			session = HibernateSessionFactory.getSession();
			Message message = (Message)session.get(Message.class, Integer.valueOf(id));
			message1 = message;
			if(session != null)
			    session.close();
			return message1;		
			 
		} catch (HibernateException e) {
			 
			e.printStackTrace();
			return null;
		}
         
    }

    public List list()
    {
        Session session = null;
        try {
			List listTemp;
			session = HibernateSessionFactory.getSession();
			Criteria criteria = session.createCriteria(Message.class);
			listTemp = criteria.list();
			return listTemp;
		} catch (HibernateException e1) {
		 
			e1.printStackTrace();
		}
        
        return null;
    }

    public List listNumbersForPage(int CurrentPage, int singlePageNumber)
    {
        try {
			Session session = null;
			List list1;
			session = HibernateSessionFactory.getSession();
			String hql = "from Message";
			List list = session.createQuery(hql).list();
			List listTemp;
			if(list.size() > CurrentPage * singlePageNumber && (CurrentPage - 1) * singlePageNumber >= 0)
			{
			    int start = (CurrentPage - 1) * singlePageNumber;
			    int end = CurrentPage * singlePageNumber;
			    listTemp = list.subList(start, end);
			} else
			{
			    listTemp = list.subList((CurrentPage - 1) * singlePageNumber, list.size());
			}
			list1 = listTemp;
			if(session != null)
			    session.close();
			return list1;
		} catch (HibernateException e) {
			 
			e.printStackTrace();return null;
		}
               
    }

    public int totalNumber()
    {
        try {
			Session session;
			session = null;
			Transaction tx = null;
			int i;
			session = HibernateSessionFactory.getSession();
			  tx = session.beginTransaction();
			String hql = "from Message";
			List list = session.createQuery(hql).list();
			int temp = list.size();
			tx.commit();
			i = temp;
			if(session != null)
			    session.close();
			return i;
		} catch (HibernateException e) {
			 
			e.printStackTrace();
			return 0;
		}
       
    }

    public boolean save(Message message)
    {
        try {
			Session session;
			session = null;
			Transaction tx = null;
			session = HibernateSessionFactory.getSession();
      tx = session.beginTransaction();
			session.save(message);
			tx.commit();
			if(session != null)
			    session.close();
			return true;
		} catch (HibernateException e) {
			 
			e.printStackTrace();return false;
		}
        
    }

    public boolean update(Message message)
    {
        try {
			Session session = null;
			Transaction tx = null;
			Message M;
			  session = HibernateSessionFactory.getSession();
			tx = session.beginTransaction();
			M = (Message)session.merge(message);
			tx.commit();
			return true;
		} catch (HibernateException e) {
			 
			e.printStackTrace();
			   return false;
		}
         
     
    }

    public void delete(Message message)
    {
        Session session = null;
        Transaction tx = null;
        try
        {
            session = HibernateSessionFactory.getSession();
            tx = session.beginTransaction();
            session.delete(message);
            tx.commit();
        }
        catch(HibernateException e)
        {
            e.printStackTrace();
        }
    }

    public void deleteById(int id)
    {
        Session session = null;
        Transaction tx = null;
        try
        {
            session = HibernateSessionFactory.getSession();
            tx = session.beginTransaction();
            Message message = (Message)session.get(Message.class, Integer.valueOf(id));
            session.delete(message);
            tx.commit();
        }
        catch(HibernateException e)
        {
            e.printStackTrace();
        }
    }
    public List<Message> findAllMessageByPage(int pageNo,int pageSize) {
		Session s = null;
		try {
			s = HibernateSessionFactory.getSession();
			int offset=(pageNo-1)*pageSize;
			String hql = "from Message as m order by m.id";
			return s.createQuery(hql).setFirstResult(offset).setMaxResults(pageSize).list();
		} finally {
			if (s != null)
				s.close();
		}
	}

	public long findCount() {
		Session s = null;
		try {
			s = HibernateSessionFactory.getSession();
			String hql = "select count(m.id) from Message as m";
			Object obj= s.createQuery(hql).uniqueResult();
			return (Long)obj;
		} finally {
			if (s != null)
				s.close();
		}
	}
}